package com.aivin.firplugin.tool;

import com.aivin.firplugin.bean.ApkInfo;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.vfs.VirtualFile;
import net.dongliu.apk.parser.ApkParser;
import net.dongliu.apk.parser.bean.ApkMeta;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 获取本地APK的信息
 */
public class ApkTool {

    public ApkInfo parseApk(String url){
        ApkParser apkParser   ;
        ApkInfo apkInfo = new ApkInfo() ;

        try {
            apkParser = new ApkParser(new File(url));
            ApkMeta apkMeta = apkParser.getApkMeta();

            apkInfo.setVersionName(apkMeta.getVersionName());
            apkInfo.setVersionCode(apkMeta.getVersionCode().toString());
            apkInfo.setBundleId(apkMeta.getPackageName());
            apkInfo.setName(apkMeta.getLabel());
            apkInfo.setIcon(apkMeta.getIcon().getPath());
            apkInfo.setFilePath(url);

            apkParser.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  apkInfo ;
    }


    public  FileChooserDescriptor createSingleFileDescriptor(final FileType fileType) {
        return new FileChooserDescriptor(
                true,
                true,
                true,
                true,
                false,
                false) {

            @Override
            public boolean isFileVisible(final VirtualFile file, final boolean showHiddenFiles) {
                return file.isDirectory() || file.getFileType() == fileType;
            }

            @Override
            public boolean isFileSelectable(final VirtualFile file) {
                return super.isFileSelectable(file) && file.getFileType() == fileType;
            }
        };
    }


    /**
     *  根据 apk 中 icon 的路径，解压出 ico文件到电脑中
     * @param name  res/mipmap-xhdpi-v4/appicon167.png
     * @return C:\Users\WA\.pgyer/ax1620107888661.png
     */
    public String queryIconPath(String apkPath , String name)  {
        int length = 0;
        byte[] b = new byte[1024];
        String path = null;
        try {
            ZipFile zipFile = new ZipFile( new File( apkPath));
            Enumeration enumeration = zipFile.entries();
            ZipEntry zipEntry ;

            while (enumeration.hasMoreElements()) {
                zipEntry = (ZipEntry) enumeration.nextElement();
                if(zipEntry.isDirectory()){
                    continue;
                }
                if ( name.equals(zipEntry.getName())) {
                    path =  getAppDataFolder().getPath() + "/firapplogo.png";
                    OutputStream outputStream = new FileOutputStream(path);
                    InputStream inputStream = zipFile.getInputStream(zipEntry);
                    while ((length = inputStream.read(b)) > 0) {
                        outputStream.write(b, 0, length);
                    }
                    outputStream.close();
                    inputStream.close();
                }
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }
        WkLog.showMsg("name=="+name +"  path=="+path);
        return path;
    }


    private  File getAppDataFolder()  {
        String os = System.getProperty("os.name").toUpperCase();
        String dataFile = (os.contains("MAC")) ? "Library/Caches/firapk" : ".firapk";
        File file = new File( System.getProperty("user.home"), dataFile);
        file.mkdirs();
        return file;
    }

}
